package com.tsd.system.service.impl;

import com.tsd.common.vo.BatchDeleteLogicVo;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.ListUtil;
import com.tsd.core.vo.PageBean;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.dao.SysDataAuthGroupMapper;
import com.tsd.system.entity.SysDataAuthExt;
import com.tsd.system.entity.SysDataAuthGroup;
import com.tsd.system.entity.SysDataAuthGroupExt;
import com.tsd.system.entity.SysUser;
import com.tsd.system.service.SysDataAuthGroupService;
import com.tsd.core.thread.ThreadPoolTaskUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 描述：SysDataAuthGroup 服务实现层
 *
 * @author admin
 * @date 2022/11/15 15:12:44
 */
@Service
public class SysDataAuthGroupServiceImpl extends BaseServiceImpl implements SysDataAuthGroupService {

    @Resource
    private SysDataAuthGroupMapper sysDataAuthGroupMapper;

    @Override
    public SysDataAuthGroup getSysDataAuthGroupById(Long id) throws Exception {
        return sysDataAuthGroupMapper.selectByPrimaryKey(id);
    }

    @Override
    public SysDataAuthGroupExt queryBySid(String sid) throws Exception {
        return sysDataAuthGroupMapper.selectBySid(sid);
    }

    @Override
    public void deleteSysDataAuthGroups(List<String> list, SysUser opUser) throws Exception {
        for (String sid : list) {
            SysDataAuthGroup old = sysDataAuthGroupMapper.selectBySid(sid);
            super.checkEmpty(old, "该记录不存在");
            super.autoInjectBaseData(old, opUser, TYPE_DELETE);
            sysDataAuthGroupMapper.updateByPrimaryKeySelective(old);
        }
    }

    @Override
    public void saveSysDataAuthGroup(SysDataAuthGroup record, SysUser opUser) throws Exception {
        if (HlpUtils.isEmpty(record.getId())) {
            super.autoInjectBaseData(record, opUser, this.TYPE_CREATE);
            sysDataAuthGroupMapper.insertSelective(record);
        } else {
            super.autoInjectBaseData(record, opUser, this.TYPE_UPDATE);
            sysDataAuthGroupMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public void saveSysDataAuthGroup(SysDataAuthExt record, SysUser opUser) throws Exception {
        List<SysDataAuthGroupExt> list = record.getGroup_list();

        SysDataAuthGroupExt params = new SysDataAuthGroupExt();
        params.setDr(0);
        params.setAuth_sid(record.getSid());
        List<SysDataAuthGroupExt> oldList = sysDataAuthGroupMapper.selectByEntity(params);

        List<Long> existIdList = ListUtil.filterMap(list, t -> !HlpUtils.isEmpty(t.getId()), SysDataAuthGroupExt::getId);
        List<SysDataAuthGroupExt> delList = ListUtil.filter(oldList, t -> !existIdList.contains(t.getId()));
        if (!HlpUtils.isEmptyList(delList)) {
            List<Long> delIdList = ListUtil.map(delList, SysDataAuthGroupExt::getId);
            BatchDeleteLogicVo logicVo = new BatchDeleteLogicVo(opUser);
            logicVo.setIdList(delIdList);
            sysDataAuthGroupMapper.deleteLogicByIdList(logicVo);
        }
        List<SysDataAuthGroupExt> insertList = ListUtil.filter(list, t -> HlpUtils.isEmpty(t.getId()));
        if (!HlpUtils.isEmptyList(insertList)) {
            for (SysDataAuthGroupExt groupExt : insertList) {
                groupExt.setAuth_sid(record.getSid());
                super.autoInjectBaseData(groupExt, opUser, this.TYPE_CREATE);
            }
            ThreadPoolTaskUtil.executeInList4Split(insertList, false, itemList -> sysDataAuthGroupMapper.insertList(itemList));
        }
    }

    @Override
    public List<SysDataAuthGroupExt> findSysDataAuthGroups(Map<String, Object> params, PageBean page) throws Exception {
        return sysDataAuthGroupMapper.selectByPage(params, page);
    }

    @Override
    public List<SysDataAuthGroupExt> findSysDataAuthGroups(Map<String, Object> params) throws Exception {
        return sysDataAuthGroupMapper.selectByParams(params);
    }
}